/**
*java里的Graphics类
*注意如果是在一个mc上画，画的东西会被mc在flash创作工具里的md盖住，bug??
*
*!!在画图之前考虑是不是要clear()!!
*/

package kgame5.kgui {
import kgame5.kutil.T;

import flash.display.*;
import flash.geom.*;

public class GP{
	
	public static function clear(g:Graphics):void{
		g.clear();
	}
	public static function drawPoint(g:Graphics,color:Number,x0:Number,y0:Number):void{
		g.lineStyle(1,color);//,100,true,"none","round","round",-1);
		g.moveTo(x0,y0);
		g.lineTo(x0+1,y0+1);
		//mc.lineTo(x0,y0);
		T.p("画了一个点");
	}
	public static function drawLine(g:Graphics,color:int,x0:Number,y0:Number,x1:Number,y1:Number):void{
		g.lineStyle(0,color);//,100,true,"none","round","round",-1);
		g.moveTo(x0,y0);
		g.lineTo(x1,y1);
	}
	public static function drawRect(g:Graphics,color:Number,x:Number,y:Number,w:Number,h:Number):void{
		g.lineStyle(1,color);//,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
		g.drawRect(x,y,w,h);
		
		/*
		var x0=x;
		var y0=y;
		var x1=x+w;
		var y1=y+h;
		
		mc.lineStyle(thickness,color,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
		mc.moveTo(x0,y0);
		mc.lineTo(x1,y0);
		mc.lineTo(x1,y1);
		mc.lineTo(x0,y1);
		mc.lineTo(x0,y0);
		//*/
	}
	public static function fillRect(g:Graphics,color:Number,x:Number,y:Number,w:Number,h:Number):void{
		g.lineStyle(undefined,color);//,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
		g.moveTo(x,y);//这个一定要在beginFill之前，否则beginFill发现不是moveTo的位置，则要自动闭和的
		g.beginFill(color);
		g.drawRect(x,y,w,h);
		g.endFill();
		
		/*
		var x0=x;
		var y0=y;
		var x1=x+w;
		var y1=y+h;
		mc.lineStyle(undefined,color,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
		mc.moveTo(x0,y0);//这个一定要在beginFill之前，否则beginFill发现不是moveTo的位置，则要自动闭和的
		mc.beginFill(color,100);
		
		mc.lineTo(x1,y0);
		mc.lineTo(x1,y1);
		mc.lineTo(x0,y1);
		mc.lineTo(x0,y0);
		mc.endFill();
		//*/
	}
	//参数是一个一维数组
	public static function drawPolygon(g:Graphics,color:Number,aPoint:Array):void{
		g.lineStyle(1,color);//,100,true,"none","round","round",-1);
		g.moveTo(aPoint[0],aPoint[1]);
		for(var i:int=2;i<aPoint.length;i+=2){
			g.lineTo(aPoint[i],aPoint[i+1]);
		}
		g.lineTo(aPoint[0],aPoint[1]);
		
	}
	public static function fillPolygon(g:Graphics,color:Number,aPoint:Array):void{
		g.lineStyle(undefined,color);//,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
		g.moveTo(aPoint[0],aPoint[1]);
		g.beginFill(color);
		
		for(var i:int=2;i<aPoint.length;i+=2){
			g.lineTo(aPoint[i],aPoint[i+1]);
		}
		g.lineTo(aPoint[0],aPoint[1]);
		g.endFill();
	}
	
	//快速填充三角形
	public static function drawTriangle(g:Graphics,color:Number,
		x0:Number,y0:Number,x1:Number,y1:Number,x2:Number,y2:Number):void{
		g.lineStyle(1,color);//,100,true,"none","round","round",-1);
		
		g.moveTo(x0,y0);
		
		g.lineTo(x1,y1);
		
		g.lineTo(x2,y2);
		
		g.lineTo(x0,y0);
		
	}
	public static function fillTriangle(g:Graphics,color:Number,
		x0:Number,y0:Number,x1:Number,y1:Number,x2:Number,y2:Number):void{
			
		g.lineStyle(undefined,color);//,100,true,"none","round","round",-1);//不设置线条粗细可是不显示的哦
	
		g.moveTo(x0,y0);
		
		g.beginFill(color);
		
		g.lineTo(x1,y1);
		g.lineTo(x2,y2);
		g.lineTo(x0,y0);
		
		g.endFill();
	}
	
	
	///////////
	public static function drawArc(g:Graphics,lineThickness:int,lineColor:Number,fillColor:Number,x:Number, y:Number, startAngle:Number, arc:Number, radius:Number, yRadius:Number):void
    {
    	//T.p(mc);
        if (arguments.length < 5)
        {
            return;
        } // end if
        g.moveTo(x, y);
        
        //是否画线
        if(lineColor<0){
        	 g.lineStyle(undefined);//不要线
        }else{
        	g.lineStyle(lineThickness,lineColor);//!!必须加上这个，否则画不出来
       }
        
        //是否填充
        if(fillColor>=0){
        	g.beginFill(fillColor);
        }
       
        if (yRadius<0)
        {
            yRadius = radius;
        } // end if
        var _loc20;
        var _loc3;
        var _loc4;
        var _loc6;
        var _loc15;
        var _loc19;
        var _loc18;
        var _loc14;
        var _loc12;
        var _loc13;
        var _loc11;
        if (Math.abs(arc) > 360)
        {
            arc = 360;
        } // end if
        _loc15 = Math.ceil(Math.abs(arc) / 45);
        _loc20 = arc / _loc15;
        _loc3 = -_loc20 / 180 * 3.141593E+000;
        _loc4 = -startAngle / 180 * 3.141593E+000;
        if (_loc15 > 0)
        {
            _loc19 = x + Math.cos(startAngle / 180 * 3.141593E+000) * radius;
            _loc18 = y + Math.sin(-startAngle / 180 * 3.141593E+000) * yRadius;
            g.lineTo(_loc19, _loc18);
            for (var _loc5 = 0; _loc5 < _loc15; ++_loc5)
            {
                _loc4 = _loc4 + _loc3;
                _loc6 = _loc4 - _loc3 / 2;
                _loc14 = x + Math.cos(_loc4) * radius;
                _loc12 = y + Math.sin(_loc4) * yRadius;
                _loc13 = x + Math.cos(_loc6) * (radius / Math.cos(_loc3 / 2));
                _loc11 = y + Math.sin(_loc6) * (yRadius / Math.cos(_loc3 / 2));
                g.curveTo(_loc13, _loc11, _loc14, _loc12);
            } // end of for
            g.lineTo(x, y);
        } // end if
        
         //是否填充
        if(fillColor>=0){
        	g.endFill();
        }
    } //
    ///////////
	public static function drawArc2(g:Graphics,lineThickness:int,lineColor:Number,fillColor:Number,x:Number, y:Number, startAngle:Number, arc:Number, radius:Number, yRadius:Number):void
    {
    	//T.p(mc);
        if (arguments.length < 5)
        {
            return;
        } // end if
        
        
        //是否画线
        if(lineColor<0){
        	 g.lineStyle(undefined);//不要线
        }else{
        	g.lineStyle(lineThickness,lineColor);//!!必须加上这个，否则画不出来
       }
        
        //是否填充
        if(fillColor>=0){
        	g.beginFill(fillColor);
        }
       
        if (yRadius<0)
        {
            yRadius = radius;
        } // end if
        var _loc20;
        var _loc3;
        var _loc4;
        var _loc6;
        var _loc15;
        var _loc19;
        var _loc18;
        var _loc14;
        var _loc12;
        var _loc13;
        var _loc11;
        if (Math.abs(arc) > 360)
        {
            arc = 360;
        } // end if
        _loc15 = Math.ceil(Math.abs(arc) / 45);
        _loc20 = arc / _loc15;
        _loc3 = -_loc20 / 180 * 3.141593E+000;
        _loc4 = -startAngle / 180 * 3.141593E+000;
        if (_loc15 > 0)
        {
            _loc19 = x + Math.cos(startAngle / 180 * 3.141593E+000) * radius;
            _loc18 = y + Math.sin(-startAngle / 180 * 3.141593E+000) * yRadius;
            g.moveTo(_loc19, _loc18);
            g.lineTo(_loc19, _loc18);
            for (var _loc5 = 0; _loc5 < _loc15; ++_loc5)
            {
                _loc4 = _loc4 + _loc3;
                _loc6 = _loc4 - _loc3 / 2;
                _loc14 = x + Math.cos(_loc4) * radius;
                _loc12 = y + Math.sin(_loc4) * yRadius;
                _loc13 = x + Math.cos(_loc6) * (radius / Math.cos(_loc3 / 2));
                _loc11 = y + Math.sin(_loc6) * (yRadius / Math.cos(_loc3 / 2));
                g.curveTo(_loc13, _loc11, _loc14, _loc12);
            } // end of for
            //g.lineTo(x, y);
        } // end if
        
         //是否填充
        if(fillColor>=0){
        	g.endFill();
        }
    } //
    public static function fillRectWithBmd(g:Graphics,rectW:Number,rectH:Number,bmd:BitmapData){
    	
    	var mtr:Matrix=new Matrix();
		mtr.scale(rectW/bmd.width, rectH/bmd.height);
		
		g.lineStyle(undefined);//不要线
		g.clear();
		g.beginBitmapFill(bmd,mtr,true,true);
		g.drawRect(0, 0, rectW, rectH);
		g.endFill();
    }
    //
    /*
	public static function Blt0(src:BitmapData,x,y,w,h,aim:BitmapData,x1,y1){
		aim.copyPixels(src,new Rectangle(x,y,w,h),new Point(x1,y1));
	}
	//*/
	//
	/**
   *  不管图片条的大小，将其等分，分割。
   * 一种用途是用做组装图片，用这个把原来的图重新组合好成为一条，这样可以减少游戏中对其的分割处理
   * @param img Image
   * @param width int
   * @param height int
   * @param rawFrameID int
   * @param screenX int 左上角
   * @param screenY int
   * @param g Graphics
   */
   /*
  public static function Blt3( src:BitmapData,width,height,frame,
                          aim:BitmapData,x1,y1
                      ) {

    var imgW = src.width;

    var temp = width * frame; //假设是一条时 此frame的左上角坐标
    var xOfs = temp % imgW;
    var yOfs = temp / imgW * height;

   aim.copyPixels(src,new Rectangle(xOfs,yOfs,width,height),new Point(x1,y1));

  }
  //*/
  /*
  //根据指定数组创建一个地图的bitmap
	public static function CreateGroundImage(a2Ground:Array,CellSize:Number,srcImage:BitmapData):BitmapData{
		var CC:Number=a2Ground[0].length;
		var RC:Number=a2Ground.length;
		//正面位图
		var aim:BitmapData=new BitmapData(CellSize*CC,CellSize*RC);//30*30一个，共5个
		
		//要贴图的
		for(var r=0;r<RC;r++){
			var y:Number=r*CellSize;
			for(var c=0;c<CC;c++){
				var x:Number=c*CellSize;
				var f:Number=a2Ground[r][c];
				GP.Blt3(srcImage,CellSize,CellSize,f,aim,x,y);
			}
		}
		return aim;
	}
	//*/
	
	 //把一个位图弄成一个倒影
	 public static function DrawRefToBmd(bmd:BitmapData,iRefColor:int){
	 	
	 	var W:Number=bmd.width;
		var H:Number=bmd.height;
					
		//这个是倒影的遮罩
		var shp_ref:Shape=new Shape();
					
		var g_ref:Graphics=shp_ref.graphics;
			
		var m:Matrix=new Matrix();
		m.createGradientBox(W,H,-Math.PI/2,0,0);
				
		g_ref.moveTo(0,0);
		//g.beginFill(0xff0000);
		g_ref.beginGradientFill(GradientType.LINEAR ,[iRefColor,iRefColor],[0.5,1],[0,80],m);
		g_ref.drawRect(0, 0, W, H);
		g_ref.endFill();
					
		bmd.draw(shp_ref);
	 	
	 }
	 
	
}//end of class 
}